package com.inspur.edp.lcm.metadata.extend.action;

import com.inspur.edp.lcm.delivery.api.entity.Deliverable;
import com.inspur.edp.lcm.delivery.spi.DeliverableDisplaySpi;
import com.inspur.edp.lcm.delivery.spi.args.DeliverableDisplayArgs;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataType;
import com.inspur.edp.lcm.metadata.api.service.MetadataProjectService;
import com.inspur.edp.lcm.metadata.api.service.MetadataService;
import com.inspur.edp.lcm.metadata.common.FileServiceImp;
import com.inspur.edp.lcm.metadata.common.Utils;
import com.inspur.edp.lcm.metadata.devcommon.ManagerUtils;
import io.iec.edp.caf.commons.utils.CollectionUtils;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MetadataDeliverableDisplayImpl implements DeliverableDisplaySpi {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private MetadataProjectService metadataProjectService;
    private MetadataService metadataService;

    private MetadataProjectService getMetadataProjectService() {
        if (metadataProjectService == null) {
            metadataProjectService = SpringBeanUtils.getBean(MetadataProjectService.class);
        }
        return metadataProjectService;
    }

    private MetadataService getMetadataService() {
        if (metadataService == null) {
            metadataService = SpringBeanUtils.getBean(MetadataService.class);
        }
        return metadataService;
    }
    /**
     * 展示元数据交付物
     *
     * @param args 参数
     * @return 展示的交付物列表
     */
    @Override
    public List<Deliverable> display(DeliverableDisplayArgs args) {
        if (args == null) {
            return new ArrayList<>();
        }
        List<Deliverable> deliverables = new ArrayList<>();
        if (CollectionUtils.isEmpty(args.getBoPathList())) {
            return deliverables;
        }
        for (String boPath : args.getBoPathList()) {
            deliverables.addAll(getDeliverables(boPath));
        }
        return deliverables;
    }

    private List<Deliverable> getDeliverables(String boPath) {
        List<Deliverable> deliverables = new ArrayList<>();
        // 获取所有工程路径
        List<String> projPaths = getMetadataProjectService().getProjPathsInPath(boPath);
        if (CollectionUtils.isEmpty(projPaths)) {
            return deliverables;
        }
        // 获取类型map
        List<MetadataType> metadataTypeList = getMetadataService().getMetadataTypeList();
        Map<String, String> metadataTypeMap = metadataTypeList.stream().collect(Collectors.toMap(MetadataType::getTypeCode, MetadataType::getTypeName));

        // 获取元数据
        for (String projPath : projPaths) {
            // 获取工程下的元数据
            List<GspMetadata> metadataList = getMetadataService().getMetadataList(projPath);
            // 相同工程路径的一批处理
            String relativeProjPath = ManagerUtils.getRelativePath(projPath);
            deliverables.addAll(metadataList.stream().map(metadata -> {
                // 获取最后修改时间
                String metadataAbsolutePath = ManagerUtils.getAbsolutePath(FileServiceImp.combinePath(metadata.getRelativePath(), metadata.getHeader().getFileName()));
                String lastChangedOn = DATE_FORMAT.format(new Date(new File(metadataAbsolutePath).lastModified()));
                // 获取最后修改人
                String lastChangedBy = Utils.getFileAttrLastChangedBy(metadataAbsolutePath);
                return new Deliverable(metadata.getHeader().getId(), metadata.getHeader().getName(), metadataTypeMap.get(metadata.getHeader().getType()), lastChangedBy, lastChangedOn, null, relativeProjPath);
            }).collect(Collectors.toList()));
        }
        return deliverables;
    }
}
